package com.hdax.ssm.service.impl;

import com.github.pagehelper.Page;
import com.github.pagehelper.PageHelper;
import com.hdax.ssm.dao.UserInfoDao;
import com.hdax.ssm.entity.UserInfo;
import com.hdax.ssm.service.UserInfoService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.web.bind.annotation.ExceptionHandler;

import javax.servlet.http.HttpServletRequest;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
@Service
public class UserInfoServiceImpl implements UserInfoService {
    @Autowired
    private UserInfoDao userInfoDao;
    @Override
    public Map<String, Object> getUserAll(int offset, int limit) {
        Map<String,Object> map = new HashMap<>();
        Page page = PageHelper.offsetPage(offset,limit,true);
        List<UserInfo> userInfoList = userInfoDao.getUserAll();
        map.put("total",page.getTotal());
        map.put("rows",userInfoList);
        return map;
    }

    @Override
    public Map<String, Object> delUser(String ids, int offset, int limit) {
        String[] strings = ids.split(",");
        userInfoDao.del(strings);
        return getUserAll(offset,limit);
    }

    @Override
    public UserInfo getUserById(long id) {
        return userInfoDao.getUserById(id);
    }

    @Override
    public Map<String,Object> saveUser(UserInfo userInfo,Integer offset,Integer limit){
        userInfoDao.addUser(userInfo);
        return getUserAll(offset,limit);
    }

    @Override
    public Map<String,Object> update(UserInfo userInfo,Integer offset,Integer limit){
        userInfoDao.update(userInfo);
        return getUserAll(offset,limit);
    }
}
